<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
    <script th:src="@{/static/jquery-2.1.4.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/mylayer.js}" type="text/javascript" charset="utf-8"></script>
</head>
<body>
    <br>
    <form class="layui-form layui-row layui-col-space16 layui-inline" style="margin-left: 20px">
        <div class="layui-inline">商品名称</div>
        <div class="layui-inline" style="width: 150px;">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-form"></i>
                </div>
                <input type="text" name="name" value="" placeholder="请输入" class="layui-input" lay-affix="clear">
            </div>
        </div>
        <div class="layui-inline">商品分类</div>
        <div class="layui-inline" style="width: 150px;">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-form"></i>
                </div>
                <select id="topCategotry" lay-filter="topCategotry">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-inline" style="width: 150px;">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-form"></i>
                </div>
                <select id="secondCategotry" name="categoryId">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-inline" style="width: 60px;">上架时间</div>
        <div class="layui-inline" style="width: 205px;">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-date"></i>
                </div>
                <input type="text" name="beginCreateTime" id="beginCreateTime" lay-verify="datetime" placeholder="开始时间" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline" style="width: 205px;">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-date"></i>
                </div>
                <input type="text" name="endCreateTime" id="endCreateTime" lay-verify="datetime" placeholder="截止时间" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn" lay-submit lay-filter="submitSearch">Search</button>
            <button type="reset" class="layui-btn layui-btn-primary" id="clear">Clear</button>
        </div>
    </form>
    <table class="layui-hide" id="test" lay-filter="test"></table>
<!--    头部工具条-->
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container layui-inline">
            <button class="layui-btn" lay-event="add">添加</button>
            <button class="layui-btn layui-btn-danger" lay-event="deleteAll">批量删除</button>
            <button type="button" class="layui-btn layui-bg-blue" lay-event="onAll">批量上架</button>
            <button type="button" class="layui-btn layui-bg-orange" lay-event="offAll">批量下架</button>
        </div>
<!--        搜索form表单-->

    </script>
<!--    右侧工具条-->
    <script type="text/html" id="barDemo">
        <div class="layui-clear-space">
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="detail">详情</a>
        </div>
    </script>
    <script type="text/html" id="imageTemplet">
        <img src="{{d.mainImage}}">
    </script>
    <script type="text/html" id="statusTemplet">
        <input type="checkbox" name="status" value="{{= d.id }}" title="上架|下架"
               lay-skin="switch" lay-filter="statusFilter" {{ d.status == 1 ? "checked" : "" }}>
    </script>
    <script type="text/html" id="isDeletedTemplet">
        <input type="checkbox" name="isDeleted" value="{{= d.id }}" title="未删除|删除"
               lay-skin="switch" lay-filter="isDeletedFilter" {{ d.isDeleted == 0 ? "checked" : "" }}>
    </script>
    <script>
        layui.use(['table', 'form', 'laydate'], function () {
            var form = layui.form;
            var table = layui.table;
            var laydate = layui.laydate;

            // 日期
            laydate.render({
                elem: '#beginCreateTime',
                type: 'datetime'
            });
            laydate.render({
                elem: '#endCreateTime',
                type: 'datetime'
            });
            $("#clear").click(function () {
                $("#secondCategotry").empty(); //会使选项清空
            })

            // 创建渲染实例
            table.render({
                elem: '#test',
                id: 'tableId',
                url:'/product/list',
                toolbar: '#toolbarDemo',
                page: true,
                cols: [
                        [
                        {type: 'checkbox', fixed: 'left'},
                        {field:'id', width:60, title: 'ID', sort: true},
                        {field:'name', title: '商品名称', width: 150},
                        {field:'categoryName', title: '商品分类', width: 80},
                        {field:'subtitle', title: '商品副标题', width: 100},
                        {field:'mainImage', title: '商品照片', templet: '#imageTemplet', width: 150},
                        {field:'price', title: '价格', align: 'center',width: 150},
                        {field:'stock', title: '库存数量',  width: 90},
                        {field:'sales', title: '销量',  width: 90},
                        {field:'status', title: '状态', templet: '#statusTemplet', align: 'center', width: 100},
                        {field:'isDeleted', title: '逻辑删除', templet: '#isDeletedTemplet', align: 'center', width: 100},
                        {field: 'createTime', title: '上架时间', width: 170},
                        {field: 'updateTime', title: '更新时间', width: 170},
                        {field:'detail', title: '商品详情', escape: false, width: 300},
                        {fixed: 'right', title:'操作', width: 150, align: 'center', minWidth: 125, toolbar: '#barDemo'}
                    ]
                ],
            });
            $.post(
                '/category/listAll',
                function (result) {
                    console.log(result);
                    if (result.code == 0) {
                        $(result.data).each(function () {
                            $('#topCategotry').append('<option value="'+this.id+'">'+this.name+'</option>');
                        })
                        form.render('select');
                    }
                },
                'json'
            );

            form.on('select(topCategotry)', function(data){
                var elem = data.elem; // 获得 select 原始 DOM 对象
                var value = data.value; // 获得被选中的值
                var othis = data.othis; // 获得 select 元素被替换后的 jQuery 对象

                $.post(
                    '/category/ByCategoryPatientId',
                    {'id': value},
                    function (result) {
                        console.log(result);
                        if (result.code == 0) {
                            $('#secondCategotry').empty();
                            $(result.data).each(function () {
                                $('#secondCategotry').append('<option value="'+this.id+'">'+this.name+'</option>');
                            })
                            form.render('select');
                        }
                    },
                    'json'
                );
            });
            form.on('switch(statusFilter)', function(obj){
                //update blog set status=? where id=1;
                var id = this.value;
                var checked = obj.elem.checked;
                console.log(checked);
                var status = checked ? 1 : 0;
                $.post(
                    '/product/updateStatus',
                    {'id': id, 'status': status},
                    function (result) {
                        if (result.code == 0) {
                            mylayer.okMsg(result.msg);
                        }
                    },
                    'json'
                );
            });
            form.on('switch(isDeletedFilter)', function(obj){
                //update blog set status=? where id=1;
                var id = this.value;
                var checked = obj.elem.checked;
                console.log(checked);
                var isDeleted = checked ? 0 : 1;
                $.post(
                    '/product/updateIsDeleted',
                    {'id': id, 'isDeleted': isDeleted},
                    function (result) {
                        if (result.code == 0) {
                            mylayer.okMsg(result.msg);
                        }
                    },
                    'json'
                );
            });
            form.on('submit(submitSearch)', function(data){
                var field = data.field; // 获得表单字段
                // 执行搜索重载
                table.reload('tableId', {
                    page: {
                        curr: 1 // 重新从第 1 页开始
                    },
                    where: field // 搜索的字段
                });
                return false; // 阻止默认 form 跳转
            });
            table.on('toolbar(test)', function(obj){
                var id = obj.config.id;
                var checkStatus = table.checkStatus(id);
                var othis = lay(this);
                switch(obj.event){
                    case 'add':
                        var index = layer.open({
                            title: '添加商品',
                            type: 2,
                            area: ['70%', '90%'],// 宽高
                            content: '/page/product/add',
                        });
                        break;
                    case 'deleteAll':
                        var data = checkStatus.data;
                        //[{"id":2,"name":"Java1812","address":"ddf","time":"232"},{"id":3,"name":"Java1903","address":"34","time":"323"}]
                        var ids = new Array();
                        $(data).each(function () {
                            ids.push(this.id);
                        })
                        //[2,4]
                        layer.confirm(
                            '您确认要删除么?',
                            {icon : 3},
                            function(index){
                                $.post(
                                    '/product/deleteAll',
                                    {'ids': ids},
                                    function (result) {
                                        console.log(result);
                                        if (result.code == 0) {
                                            mylayer.okMsg(result.msg);
                                            //删除成功之后刷新表格，展示最新数据
                                            table.reload('tableId');
                                        } else {
                                            mylayer.errorMsg(result.msg);
                                        }
                                    },
                                    'json'
                                );
                            }
                        );
                        break;
                    case 'onAll':
                        var data = checkStatus.data;
                        //[{"id":2,"name":"Java1812","address":"ddf","time":"232"},{"id":3,"name":"Java1903","address":"34","time":"323"}]
                        var ids = new Array();
                        $(data).each(function () {
                            ids.push(this.id);
                        })
                        //[2,4]
                        $.post(
                            '/product/onAll',
                            {'ids': ids},
                            function (result) {
                                console.log(result);
                                if (result.code == 0) {
                                    mylayer.okMsg(result.msg);
                                    //删除成功之后刷新表格，展示最新数据
                                    table.reload('tableId');
                                } else {
                                    mylayer.errorMsg(result.msg);
                                }
                            },
                            'json'
                        );
                        break;
                    case 'offAll':
                        var data = checkStatus.data;
                        //[{"id":2,"name":"Java1812","address":"ddf","time":"232"},{"id":3,"name":"Java1903","address":"34","time":"323"}]
                        var ids = new Array();
                        $(data).each(function () {
                            ids.push(this.id);
                        })
                        //[2,4]
                        $.post(
                            '/product/offAll',
                            {'ids': ids},
                            function (result) {
                                console.log(result);
                                if (result.code == 0) {
                                    mylayer.okMsg(result.msg);
                                    //删除成功之后刷新表格，展示最新数据
                                    table.reload('tableId');
                                } else {
                                    mylayer.errorMsg(result.msg);
                                }
                            },
                            'json'
                        );
                        break;
                    case 'LAYTABLE_TIPS':
                        layer.alert('自定义工具栏图标按钮');
                        break;
                };
            });
            table.on('tool(test)', function(obj){ // 双击 toolDouble
                var data = obj.data; // 获得当前行数据
                console.log(obj);
                if(obj.event === 'delete'){
                    layer.confirm(
                        '您确定要删除吗？',
                        {icon : 3},
                        function (index) {
                            $.post(
                                '/product/deleteById',
                                {'id':data.id},
                                function (result) {
                                    console.log(result);
                                    if (result.code == 0) {
                                        mylayer.okMsg(result.msg);
                                        table.reload('tableId');
                                    } else {
                                        mylayer.errorMsg(result.msg);
                                    }
                                },
                                'json'
                            );
                        }
                    );
                } else if(obj.event === 'edit'){
                    var index = layer.open({
                        title: '修改商品',
                        type: 2,
                        area: ['70%', '90%'],// 宽高
                        content: '/page/product/update?id='+data.id,
                    });
                } else if (obj.event === 'detail'){
                    layer.open({
                        title: data.name,
                        type: 1,
                        area: ['800px', '600px'],// 宽高
                        content: data.detail,
                    })
                }
            });
        });
    </script>
</body>
</html>
